<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="车名" prop="seriesName">
        <el-input
          v-model="queryParams.seriesName"
          placeholder="请输入车名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="品牌/车系" prop="fctId" label-width="120px">
        <el-select
          v-model="queryParams.fctId"
          filterable
          clearable
          remote
          reserve-keyword
          placeholder="请输入关键词"
          :remote-method="remoteMethod"
          :loading="loading">
          <el-option
            v-for="item in carFctList"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="在售状态" prop="state">
        <el-select v-model="queryParams.state"  placeholder="请选择在售状态">
          <el-option
            v-for="item in salsStatus"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:carSeries:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:carSeries:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:carSeries:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:carSeries:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"  :pageName="$options.name" ></right-toolbar>
    </el-row>

    <el-table border v-loading="loading" :data="carSeriesList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id"  v-if="columns[0].visible" show-overflow-tooltip />
      <el-table-column label="品牌" align="center" prop="brandName" v-if="columns[1].visible" show-overflow-tooltip />
      <el-table-column label="车系" align="center" prop="fctName" v-if="columns[1].visible" show-overflow-tooltip />
      <el-table-column label="车名" align="center" prop="seriesName" v-if="columns[2].visible" show-overflow-tooltip />
      <el-table-column label="最高售价" align="center" prop="seriesPriceMax" v-if="columns[4].visible" show-overflow-tooltip />
      <el-table-column label="最低售价" align="center" prop="seriesPriceMin" v-if="columns[5].visible" show-overflow-tooltip />
      <el-table-column label="在售状态" align="center" prop="state" v-if="columns[7].visible" show-overflow-tooltip >
        <template slot-scope="scope">
          <span v-if="scope.row.state === 10">未售</span>
          <span v-if="scope.row.state === 20">在售</span>
          <span v-if="scope.row.state === 40">停售</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="fctId" align="center" prop="fctId" v-if="columns[6].visible" show-overflow-tooltip />-->
<!--      <el-table-column label="seriesId" align="center" prop="seriesId" v-if="columns[8].visible" show-overflow-tooltip />-->
      <el-table-column label="pnglogo" align="center" prop="pnglogo" v-if="columns[3].visible" show-overflow-tooltip />
      <el-table-column label="图片地址" align="center" prop="seriesImg" v-if="columns[1].visible" show-overflow-tooltip />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:carSeries:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:carSeries:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改车系名称对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="品牌/车系" prop="fctId">
          <el-select
            v-model="form.fctId"
            filterable
            clearable
            remote
            reserve-keyword
            placeholder="请输入关键词"
            :remote-method="remoteMethod"
            :loading="loading">
            <el-option
              v-for="item in carFctList"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="车名" prop="seriesName">
          <el-input v-model="form.seriesName" placeholder="请输入车名" />
        </el-form-item>
        <el-form-item label="最高售价" prop="seriesPriceMax">
          <el-input-number :min="1" v-model="form.seriesPriceMax" placeholder="请输入最高售价" />
        </el-form-item>
        <el-form-item label="最低售价" prop="seriesPriceMin">
          <el-input-number :min="1" v-model="form.seriesPriceMin" placeholder="请输入最低售价" />
        </el-form-item>
        <el-form-item label="在售状态" prop="state">
          <el-select v-model="form.state"  placeholder="请选择在售状态">
            <el-option
              v-for="item in salsStatus"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
<!--        <el-form-item label="seriesId" prop="seriesId">-->
<!--          <el-input v-model="form.seriesId" placeholder="请输入seriesId" />-->
<!--        </el-form-item>-->
        <el-form-item label="pnglogo" prop="pnglogo">
          <el-input v-model="form.pnglogo" placeholder="请输入pnglogo" />
        </el-form-item>
        <el-form-item label="图片地址" prop="seriesImg">
          <el-input v-model="form.seriesImg" placeholder="请输入图片地址" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCarSeries, getCarSeries, delCarSeries, addCarSeries, updateCarSeries } from "@/api/system/carSeries";
import { listCarFct, getCarFct, delCarFct, addCarFct, updateCarFct } from "@/api/system/carFct";
export default {
  name: "CarSeries",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      salsStatus: [
        {
          value: 10,
          label: '未售'
        },  {
          value: 20,
          label: '在售'
        },  {
          value: 40,
          label: '停售'
        }],
      carFctList: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 车系名称表格数据
      carSeriesList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        seriesImg: null,
        seriesName: null,
        pnglogo: null,
        seriesPriceMax: null,
        seriesPriceMin: null,
        fctId: null,
        state: null,
        seriesId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      // 列表的列的显示隐藏设置
      columns:[
                  { key: 0, label: `id`, visible: true },
                  { key: 1, label: `图片地址`, visible: true },
                  { key: 2, label: `车名`, visible: true },
                  { key: 3, label: `pnglogo`, visible: true },
                  { key: 4, label: `最高售价`, visible: true },
                  { key: 5, label: `最低售价`, visible: true },
                  { key: 6, label: `品牌id`, visible: true },
                  { key: 7, label: `在售状态`, visible: true },
                  { key: 8, label: `seriesId`, visible: true },
      ],
    };
  },
  created() {
    this.getList();
    //列表分页列的动态显示配置
    var columns = JSON.parse(localStorage.getItem(this.$options.name));
    if(columns){
      this.columns = columns;
    }
  },
  methods: {
    /** 查询车系名称列表 */
    getList() {
      this.loading = true;
      listCarSeries(this.queryParams).then(response => {
        this.carSeriesList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        seriesImg: null,
        seriesName: null,
        pnglogo: null,
        seriesPriceMax: null,
        seriesPriceMin: null,
        fctId: null,
        state: null,
        seriesId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    getCarFctList(params) {
      listCarFct(params).then(response => {
        for(let item of response.rows) {
          item.label =item.brandName + " / " + item.fctname;
          item.value = item.fctId;
        }
        this.carFctList = response.rows;
        this.loading = false;
      });
    },
    //远程搜索品牌数据
    remoteMethod(query) {
      let params = {
        fctname: query
      }
      console.log(params);
      if (query !== '') {
        this.loading = true;
        this.getCarFctList(params);
      } else {
        this.carBrandList = [];
      }
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加车系名称";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      this.carFctList.push({
        label : row.brandName+" / "+row.fctName,
        value :  row.fctId,
      })
      getCarSeries(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改车系名称";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCarSeries(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.seriesId = 1000;
            addCarSeries(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除车系名称编号为"' + ids + '"的数据项？').then(function() {
        return delCarSeries(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/carSeries/export', {
        ...this.queryParams
      }, `carSeries_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
